package cn.appsys.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.catalina.User;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.pojo.DevUser;
import cn.appsys.service.dev.DevUserService;
import cn.appsys.tools.Constants;


@Controller
@RequestMapping("/dev")
public class DevLoginController {
private Logger logger = Logger.getLogger(DevLoginController.class);
	
	@Resource
	private DevUserService devUserService;
	
	@RequestMapping(value="/dologin",method=RequestMethod.POST)
	public String doLogin(@RequestParam String devCode,@RequestParam String devPassword,HttpServletRequest request,HttpSession session) throws Exception{
		logger.debug("doLogin====================================");
		//调用service方法，进行用户匹配
		DevUser user =devUserService.login(devCode,devPassword);
		if(null != user){//登录成功
			//放入session
			session.setAttribute(Constants.USER_SESSION, user);
			//页面跳转（frame.jsp）
			return "redirect:/dev/sys/main";
		}else{
			//页面跳转（login.jsp）带出提示信息--转发
			request.setAttribute("error", "用户名或密码不正确");
			return "devlogin";
		}
	}
	
	
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		
		session.removeAttribute(Constants.USER_SESSION);
		return "devlogin";
	}
	@RequestMapping(value="/sys/main")
	public String main(){
		return "/developer/main";
	}
	
	@RequestMapping(value="/syserror")
	public String sysError(){
		return "syserror";
	}
}
